package org.belkodevelop.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.belkodevelop.controller.helper.AuthUtils;
import org.belkodevelop.photo.domen.Photo;
import org.belkodevelop.photo.dto.*;
import org.belkodevelop.realty.domen.Realty;
import org.belkodevelop.realty.service.RealtyService;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author VMozolev
 */
@RequestMapping("/photo")
@Controller
public class PhotoController {

    private final Log log = LogFactory.getLog(getClass());

    @Autowired
    private org.belkodevelop.photo.service.PhotoService photoService;

    @Autowired
    private RealtyService realtyService;

    @Autowired
    private DozerBeanMapper mapper;

    @Autowired
    private AuthUtils authUtils;

    @RequestMapping(value = "/postPhoto.do", method = RequestMethod.POST)
    @ResponseBody
    public PhotoUploadStatus postPhoto(PhotoUploadDto uploadItemDto) {
        try {
            Photo photo= photoService.addPhoto(uploadItemDto.getFile(), uploadItemDto.getRealtyId(), uploadItemDto.getDescription());

            return new PhotoUploadStatus(false, photo.getId());
        } catch (Exception ex) {
            log.warn("", ex);
            return new PhotoUploadStatus(true, ex.getMessage(), null);
        }
    }

    @RequestMapping(value = "/getPhotos.do", method = RequestMethod.GET)
    @ResponseBody
    public List<PhotoDto> getPhotos(Long realtyId) {
        List<Photo> photos = photoService.getPhotos(realtyId);
        List<PhotoDto> photoDtos = new ArrayList<PhotoDto>(photos.size());

        Realty realty = realtyService.getRealtyById(realtyId);

        for (Photo photo: photos) {
            PhotoDto photoDto = mapper.map(photo, PhotoDto.class);

            if (realty.getMainPhoto() != null && realty.getMainPhoto().getId() == photo.getId()) {
                photoDto.setMainPhoto(true);
            }
            photoDtos.add(photoDto);
        }
        return photoDtos;
    }

    @RequestMapping(value = "/deletePhoto.do", method = RequestMethod.GET)
    @ResponseBody
    public PhotoDeleteStatus deletePhoto(long photoId) {
        try {
            boolean result = photoService.deletePhoto(photoId, authUtils.getCurrentSessionUser());
            if (result) {
                return new PhotoDeleteStatus(false);
            } else {
                return new PhotoDeleteStatus(true, "wrong id photo");
            }
        } catch (Exception ex) {
            log.warn("", ex);
            return new PhotoDeleteStatus(true, ex.getMessage());
        }
    }

    @RequestMapping(value = "/setMainPhoto.do", method = RequestMethod.GET)
    @ResponseBody
    public SetMainPhotoStatus setMainPhoto(long realtyId, Long photoId) {
        try {
            boolean result = photoService.setMainPhoto(photoId, realtyId, authUtils.getCurrentSessionUser());
            if (result) {
                return new SetMainPhotoStatus(false);
            } else {
                return new SetMainPhotoStatus(true, "wrong id photo");
            }
        } catch (Exception ex) {
            log.warn("", ex);
            return new SetMainPhotoStatus(true, ex.getMessage());
        }
    }
}
